// JavaScript Document
/*Definizione delle variabile globali alla pagina*/
/*State variables*/
var answer = ""; 		//this field has to be filled with information that has to be send

/*Server variables*/
var txt = "";   		 	//game name (txt)
var interrogation = ""; 	//game description (HTML)  										
var description = ""; 	//game help (HTML)												
//var fileJs = "";			//URI js file (contains game js implementation)	 (optional)
//var fileCss = "";			//URI css file (contains game dom css)	 (optional)
var iDocument = ""; 		//current question (sintax specified by the programmer)        	
var questionId = ""; 	//current question Instance ID (num)        	
var knowledge = ""; 		//answer saved on db (optional)									
/* var info = "";*/				//various (HTML)(optional)
var sessionId = ""; 		//session id (num)

/*Settings */
var server_response = "game1.aspx"; 	//server address for game init and next game request
var server_onInit = "game1.aspx"; 		//server address for game init and next game request
/*var printDiv = "#response";*/						//default server (next game) response div id
var scoreDiv = "#myScore"; 					//default score div id
var game_next = "next"; 						//init game and next game event name
var sendAns = "gameResult"; 					//submit event name
var showSubmit = true; 						//disable "Invia" button, if false the programmer have to invoke the send funcition
var check = true;

/* PROVVISORIO: da aggiungere oggetto SCORE */
var prevText = "Score: ";
var afterText;


$(document).ready(function() {

    $("#nextChallenge").hide();
    $("#sendAnswer").hide();
    $(scoreDiv).hide();

    $("#gameInit").click(function() {
        $("#gameInit").hide();

        /* programmer of the game could hide the submit button and invoke send function by himself */
        if (showSubmit) {
            $("#sendAnswer").show();
        }
        getFields();
        if (gameInit_click)
            gameInit_click();
    });

    $("#nextChallenge").click(function() {

        getFields(true);
        $(scoreDiv).hide();
        $("#nextChallenge").hide();
        if (showSubmit) {
            $("#sendAnswer").show();
        }
        if (nextChallenge_click)
            nextChallenge_click();
    });

    $("#sendAnswer").click(function() {
        /* Answer cooking */

        prepareAnswer();
        check = checkAnswer(answer);

        if (check == -2) {

            if (sendAnswer_click)
                sendAnswer_click();
            if (showSubmit) {
                $("#sendAnswer").hide();
            }

            $("#nextChallenge").show();
            send(scoreDiv);
        }

    });
    /* function for the instruction that the user want on ready */
    if (onready_user) {
        onready_user();
    }
});

function getFields(isnext) {

    messaggio = "{\"event\":\"" + game_next + "\"}";
    $.post(server_onInit, messaggio, function(data) {

        var myData = jQuery.parseJSON(data);
        alert(data);
        txt = myData.txt;
        interrogation = myData.interrogation;
        description = myData.description;
        //fileJs = myData.filejs;          // no more needed
        //fileCss = myData.filecss;	 	// no more needed
        iDocument = myData.idocument;
        questionId = myData.questionid;
        knowledge = myData.knowledge;
        /* info = myData.info; */
        sessionId = myData.sessionid;

        /* no more needed
        if(isnext){
        //load js file (optional)
        loadJs(fileJs);
					 
					 //load css file (optional)
        loadCss(fileCss);
        }
        */

        //print data loaded
        stampa();

    });

}
/*function no more needed
function loadJs(myfile){
//controllo campo non null
if(myfile != null){
//carico file
$.ajax({
type: "GET",
url: myfile,
dataType: "script",
error: function(){
alert("Impossibile caricare il file javascript!\n Provare a ricaricare la pagina. \n(Se il problema persiste consultare il medico curante o il webmaster del sito cronachemaceratesi.it)");
},
success: function(msg){
					 
					 
}
});
}// else no js file selected
};
*/


/*function no more needed
//non funziona!
function loadCss(myfile){
//controllo campo non null
if(myfile != null){
//carico file
$.ajax({
type: "GET",
url: myfile,
dataType: "html",
error: function(){
alert("Impossibile caricare il file css!\n Provare a ricaricare la pagina. \n(Se il problema persiste consultare il medico curante o il webmaster del sito cronachemaceratesi.it)");
},
success: function(msg){
					 
					 
}
});
}// else no css file selected
};
*/

function send(printHere) {
    /* Answer to send */
    answer = "{" + answer + "}";

    /* Send data*/
    message = "{\"event\":\"" + sendAns + "\",\"answer\":\"" + answer + "\",\"qiid\":\"" + questionId + "\"}";

    $.post(server_response, message, function(newScore) {

        var myscore = jQuery.parseJSON(newScore);

        /* Update user score */
        $(printHere).html(((prevText) ? prevText : "") + myscore.point + ((afterText) ? afterText : ""));
        $(printHere).show();
        if (!showSubmit) {
            $("#nextChallenge").show();
            $("#sendAnswer").hide();
        }
    });
}

